﻿Option Strict On
Imports s3pi.Package
Imports s3pi.Interfaces
Imports s3pi.DefaultResource
Imports s3pi.WrapperDealer
Imports CASP.packInteraction
Imports CASPartResource.CASPartResource
Imports System.IO
Imports CASP


Public Class Form1
    Dim newPacktoRead As IPackage
    Dim list As New ArrayList
    Dim LeftCount As Integer = 0
    Dim RightCount As Integer = 0
    Dim TotalCount As Integer
    Dim UserSet As IPackage
    Dim overridePath As String = Path.Combine(packInteraction.ModFolder, "Packages")
    Dim listBox As New ListView
    Dim ItemList As New List(Of LstItem)
    Dim Type As String
    Dim Main As String
    Dim PackName As String


    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown


        'Set the user name
        If My.Settings.UserName = "" Then
            My.Settings.UserName = Environment.UserName.ToString
        End If

        'Set the user package
        SetUserPackage()
        UserSet = Package.NewPackage(1)
        Type = "All"
        Main = "All"
        'Get the exception
        Try
            coboxPack.SelectedIndex = 0
            combMain.SelectedIndex = 0
            comboSort.SelectedIndex = 0

            PackName = coboxPack.Text

            Wait.Show()
            Wait.Focus()
            Me.Enabled = False
            bgwLoad.RunWorkerAsync()
        Catch ex As Exception
            MessageBox.Show("It seems that there is something wring while loading. Please replace DefaultResource.dll with that from s3pi or s3pe folder.")
        End Try



        'Set the UI
        Me.CenterToScreen()
        GetPack()
        SetSort()



    End Sub

#Region "No Use"
    ' ''' <summary>
    ' ''' This is to read custom content in DCcache
    ' ''' </summary>
    ' ''' <param name="Pack">package</param>
    ' ''' <remarks>Note : this has got a exception n Consort's pc</remarks>
    'Private Sub SetUp(ByVal Pack As IPackage)

    '    'This is to reset all the UI setting
    '    Reset()
    '    list.Clear()
    '    btnLeft.Enabled = False
    '    btnRight.Enabled = False
    '    PicBox.Image = Nothing
    '    RightCount = 0
    '    LeftCount = 0
    '    lstView.Items.Clear()
    '    imgList.Images.Clear()

    '    Dim n As Integer = 0

    '    For Each entry As IResourceIndexEntry In Pack.GetResourceList
    '        If entry.ResourceType = &H34AEECB Then

    '            Dim _ListCAS As CASPartResource.CASPartResource
    '            Dim _Stream As Stream = WrapperDealer.GetResource(1, Pack, entry, True).Stream

    '            If My.Computer.FileSystem.FileExists(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package")) = True Then
    '                Dim UserSetCopy As IPackage = Package.OpenPackage(1, Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"), True)
    '                For Each entryUser As IResourceIndexEntry In UserSetCopy.GetResourceList
    '                    If entryUser.Instance = entry.Instance Then
    '                        _Stream = WrapperDealer.GetResource(1, UserSetCopy, entryUser, True).Stream
    '                    End If
    '                Next

    '            End If



    '            _ListCAS = New CASPartResource.CASPartResource(1, _Stream)
    '            Dim Type As UInt32() = New UInt32() {&H2E75C765, &H2E75C765}
    '            Dim Entries As IList(Of IResourceIndexEntry) = Pack.FindAll(Function(IEntry As IResourceIndexEntry) Type.Contains(IEntry.ResourceType))

    '            'For Each entryImage As IResourceIndexEntry In Entries
    '            For Each entryImage As IResourceIndexEntry In Pack.GetResourceList
    '                If entryImage.Instance = entry.Instance And entryImage.ResourceType = &H626F60CE Then

    '                    imgList.Images.Add(System.Drawing.Image.FromStream(WrapperDealer.GetResource(1, Pack, entryImage, True).Stream))


    '                    Dim lstItem As New LstItem
    '                    With lstItem
    '                        .ImageIndex = n
    '                        .Instance = entry.Instance
    '                        .CaspList = _ListCAS
    '                        .entryKey = DirectCast(entry, IResourceKey)
    '                        .ToolTipText = "Instance: 0x" & Hex(entry.Instance) & vbNewLine & .CaspList.AgeGender.Age.ToString _
    '                        & vbNewLine & .CaspList.AgeGender.Gender.ToString
    '                        .OriginalHash = .CaspList.ToString.GetHashCode
    '                        If InStr(.CaspList.AgeGender.Gender.ToString.ToLower, "female") <> 0 Then
    '                            .Group = lstView.Groups(0)
    '                        Else
    '                            .Group = lstView.Groups(1)
    '                        End If
    '                    End With
    '                    lstView.Items.Add(lstItem)

    '                    n += 1
    '                    Exit For
    '                End If
    '            Next
    '        End If
    '    Next
    '    lblCount.Text = lstView.Items.Count
    'End Sub
#End Region

    Private Sub lstView_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstView.SelectedIndexChanged
        Reset()
        list.Clear()
        btnLeft.Enabled = False
        btnRight.Enabled = False
        PicBox.Image = Nothing
        RightCount = 0
        LeftCount = 0
        For Each lstItem As LstItem In lstView.SelectedItems

            Dim typeThum As ULong() = New ULong() {lstItem.Instance}
            Dim EntriesThum As IList(Of IResourceIndexEntry) = newPacktoRead.FindAll((Function(Entryfind As IResourceIndexEntry) typeThum.Contains(Entryfind.Instance)))

            For Each entry As IResourceIndexEntry In EntriesThum
                If entry.ResourceType = &H626F60CE Then
                    Dim _Stream As Stream = WrapperDealer.GetResource(1, newPacktoRead, entry, True).Stream
                    list.Add(_Stream)
                End If
            Next
            If list.Count > 1 Then
                btnRight.Enabled = True
                TotalCount = list.Count
            End If
            PicBox.Image = Image.FromStream(CType(list.Item(0), IO.Stream))

            ReadCasp(lstItem)
            SetFileProperty(lstItem)
        Next
    End Sub

    Private Sub btnRight_Click(sender As Object, e As EventArgs) Handles btnRight.Click
        RightCount += 1
        LeftCount = RightCount - 1
        PicBox.Image = Image.FromStream(CType(list.Item(RightCount), IO.Stream))
        If RightCount >= TotalCount - 1 Then
            btnRight.Enabled = False
            RightCount = TotalCount - 1
        End If
        btnLeft.Enabled = True
    End Sub

    Private Sub btnLeft_Click(sender As Object, e As EventArgs) Handles btnLeft.Click
        PicBox.Image = Image.FromStream(CType(list.Item(LeftCount), IO.Stream))
        LeftCount -= 1
        If LeftCount < 0 Then
            btnLeft.Enabled = False
            RightCount = 0
            LeftCount = 0
        End If
        btnRight.Enabled = True
    End Sub

    Private Sub ReadCasp(ByVal lstItem As LstItem)
        For Each ckb As CheckBox In Me.GroupBox1.Controls
            SetCKB(ckb, lstItem)
        Next
        For Each ckb As CheckBox In Me.GroupBox2.Controls
            SetCKB(ckb, lstItem)
        Next
        For Each ckb As CheckBox In Me.GroupBox3.Controls
            SetCKB(ckb, lstItem)
        Next
        For Each ckb As CheckBox In Me.GroupBox4.Controls
            SetCKB(ckb, lstItem)
        Next
        If InStr(lstItem.CaspList.ClothingCategory.ToString.ToLower, "Outerwear") <> 0 Then
            ckbOuterwear.Checked = True
        End If
    End Sub

    Private Sub Reset()
        For Each ckb As CheckBox In Me.GroupBox1.Controls
            ckb.Checked = False
        Next
        For Each ckb As CheckBox In Me.GroupBox2.Controls
            ckb.Checked = False
        Next
        For Each ckb As CheckBox In Me.GroupBox3.Controls
            ckb.Checked = False
        Next
        For Each ckb As CheckBox In Me.GroupBox4.Controls
            ckb.Checked = False
        Next
    End Sub

    Private Sub SetCKB(ByVal ckb As CheckBox, ByVal lstItem As LstItem)
        Dim name As String = ckb.Name.Replace("ckb", "")
        Dim strName As String = lstItem.CaspList.AgeGender.ToString & lstItem.CaspList.ClothingCategory.ToString & lstItem.CaspList.Clothing.ToString
        If InStr(strName, name) <> 0 Then
            ckb.Checked = True
        End If
    End Sub

    Private Sub SetFileProperty(ByVal lstItem As LstItem)
        Dim name As String = lstItem.CaspList.Unknown1
        Me.Text = "Easy CASP Editor" + ": " + name
        If lstItem.IsModified = False Then
            ckbIsModified.Checked = False
        Else
            ckbIsModified.Checked = True
        End If

    End Sub

    ''' <summary>
    ''' This is to set the user package in MODs folder, and generate a folder if the p
    ''' package folder doesn't exist
    ''' </summary>
    ''' <remarks>Onli for safety</remarks>
    Private Sub SetUserPackage()
        If My.Computer.FileSystem.DirectoryExists(overridePath) = False Then
            My.Computer.FileSystem.CreateDirectory(overridePath)
        End If

    End Sub

    ''' <summary>
    ''' This method is intended to save the changes users set
    ''' </summary>
    ''' <remarks>it now has a override function</remarks>
    Private Sub Save()
        If DirectCast(DetectSave(), ICollection).Count > 0 Then
            If My.Computer.FileSystem.FileExists(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package")) = True Then
                UserSet = Package.OpenPackage(1, Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"), True)
            Else
                UserSet = Package.NewPackage(1)
                UserSet.SaveAs(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"))
                UserSet = Package.OpenPackage(1, Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"), True)
            End If




            For Each LstItem As LstItem In DetectSave()
                For Each entry As IResourceIndexEntry In UserSet.GetResourceList
                    If entry.Instance = LstItem.Instance Then
                        If MessageBox.Show(Localize.Localize("I detect that you have made same changes to the same thing. Click yes to override the original one"), Localize.Localize("Attention"), MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                            UserSet.DeleteResource(entry)
                        End If
                    End If
                Next
                UserSet.AddResource(LstItem.entryKey, LstItem.CaspList.Stream, True)

                'set the hash code to default
                LstItem.OriginalHash = LstItem.CaspList.ToString.GetHashCode

            Next
        End If

        For Each entry As IResourceIndexEntry In UserSet.GetResourceList
            If entry.Compressed = 0 Then
                entry.Compressed = &HFFFF
            End If
        Next

        If UserSet.GetResourceList.Count > 0 Then
            UserSet.SavePackage()

        End If

    End Sub

    Private Sub Save(ByVal filePath As String)

    End Sub


    Private Sub SavePackageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SavePackageToolStripMenuItem.Click
        Save()
        MessageBox.Show("Completed!")

    End Sub

    ''' <summary>
    ''' This is to detect whether it has changed
    ''' </summary>
    ''' <returns>return all the items that has been changed</returns>
    ''' <remarks></remarks>
    Private Function DetectSave() As List(Of LstItem)
        Dim List As New List(Of LstItem)
        For Each LstItem As LstItem In lstView.Items
            If LstItem.OriginalHash <> LstItem.CaspList.ToString.GetHashCode Then
                List.Add(LstItem)
            End If
        Next
        Return List
    End Function

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        If DirectCast(DetectSave(), ICollection).Count > 0 Then ' And MessageBox.Show("You have unsaved changes. Would you like to save your change? Click yes to save", "Attention", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            Save()
        End If
    End Sub

    Private Sub GetPack()
        For Each Name As String In packInteraction.PackCollection

            coboxPack.Items.Add(Name)
        Next

    End Sub

    ''' <summary>
    ''' No Problem Now!!!
    ''' </summary>
    ''' <param name="sender">package</param>
    ''' <param name="e">eventArgs</param>
    ''' <remarks>Has re-written</remarks>
    Public Sub bgwLoad_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgwLoad.DoWork
        packInteraction.CustomPackPath = Path.Combine(overridePath, My.Settings.UserName & "_CASP.package")

        Dim pack As IPackage = Package.NewPackage(1)
        ItemList = New List(Of LstItem)()
        If InStr(PackName, "Content") = 0 Then
            pack = packInteraction.GetEAstuff(PackName)
        ElseIf InStr(PackName, "Package") <> 0 Then
            pack = packInteraction.GetCustomPackage
        Else
            pack = packInteraction.GetDBCpackage
        End If
        newPacktoRead = pack

        'Set the resource list
        Dim typeCasp As UInt32() = New UInt32() {&H34AEECB}

        Dim EntriesCasp As IList(Of IResourceIndexEntry) = pack.FindAll((Function(Entry As IResourceIndexEntry) typeCasp.Contains(Entry.ResourceType)))


        Dim n As Integer = 0
        For Each entry As IResourceIndexEntry In EntriesCasp
            Dim _ListCAS As CASPartResource.CASPartResource
            'Set the main CASP Item
            Dim lstItem As New LstItem

            Dim _Stream As Stream = WrapperDealer.GetResource(1, pack, entry, True).Stream
            If My.Computer.FileSystem.FileExists(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package")) = True Then
                Dim UserSetCopy As IPackage = Package.OpenPackage(1, Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"), True)
                For Each entryUser As IResourceIndexEntry In UserSetCopy.GetResourceList
                    If entryUser.Instance = entry.Instance Then
                        _Stream = WrapperDealer.GetResource(1, UserSetCopy, entryUser, True).Stream
                        lstItem.IsModified = True
                    End If
                Next

            End If


            _ListCAS = New CASPartResource.CASPartResource(1, _Stream)

            
            With lstItem
                .Instance = entry.Instance
                .CaspList = _ListCAS
                .entryKey = DirectCast(entry, IResourceKey)
                .ToolTipText = "Instance: 0x" & Hex(entry.Instance) & vbNewLine & .CaspList.AgeGender.Age.ToString _
                & vbNewLine & .CaspList.AgeGender.Gender.ToString
                .OriginalHash = .CaspList.ToString.GetHashCode
                If InStr(.CaspList.AgeGender.Gender.ToString, "Female") <> 0 And InStr(.CaspList.AgeGender.Gender.ToString, "Male") = 0 Then
                    .Group = lstView.Groups(0)
                ElseIf InStr(.CaspList.AgeGender.Gender.ToString, "Male") <> 0 And InStr(.CaspList.AgeGender.Gender.ToString, "Female") = 0 Then
                    .Group = lstView.Groups(1)
                Else
                    .Group = lstView.Groups(2)
                End If
            End With


            'Set the Img preview
            Dim typeThum As ULong() = New ULong() {entry.Instance}
            Dim EntriesThum As IList(Of IResourceIndexEntry) = pack.FindAll((Function(Entryfind As IResourceIndexEntry) typeThum.Contains(Entryfind.Instance)))


            For Each entryThum As IResourceIndexEntry In EntriesThum

                If entryThum.ResourceType = &H626F60CE Then
                    imgList.Images.Add(System.Drawing.Image.FromStream(WrapperDealer.GetResource(1, pack, entryThum, True).Stream))
                    lstItem.ImageIndex = n

                    'this is to cater the sort function
                    If Type = "All" Then
                        If Main = "All" Then
                            ItemList.Add(lstItem)

                        ElseIf InStr(lstItem.CaspList.AgeGender.ToString & lstItem.CaspList.ClothingCategory.ToString & lstItem.CaspList.Clothing.ToString, Main) <> 0 Then
                            ItemList.Add(lstItem)
                        End If
                    ElseIf Main = "All" Then
                        If InStr(lstItem.CaspList.AgeGender.ToString & lstItem.CaspList.ClothingCategory.ToString & lstItem.CaspList.Clothing.ToString, Type.Replace(" ", "")) <> 0 Then
                            ItemList.Add(lstItem)

                            
                        End If
                    Else
                        If InStr(lstItem.CaspList.AgeGender.ToString & lstItem.CaspList.ClothingCategory.ToString & lstItem.CaspList.Clothing.ToString, Type.Replace(" ", "")) <> 0 _
                                                    AndAlso InStr(lstItem.CaspList.AgeGender.ToString & lstItem.CaspList.ClothingCategory.ToString & lstItem.CaspList.Clothing.ToString, Main.Replace(" ", "")) <> 0 Then
                            ItemList.Add(lstItem)
                        End If

                    End If

                    n += 1


                        Exit For
                    End If
            Next
        Next


        'Reset the count
        lblCount.Text = CStr(lstView.Items.Count)


        Wait.Dispose()


    End Sub

#Region "This the part of set"
    Private Sub ckbFemale_CheckedChanged(sender As Object, e As EventArgs) Handles ckbFemale.MouseClick

        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbFemale.Checked = True Then
                lstitem.CaspList.AgeGender.Gender = CType(lstitem.CaspList.AgeGender.Gender + CASPartResource.GenderFlags.Female, CASPartResource.GenderFlags)
            ElseIf ckbFemale.Checked = False Then
                lstitem.CaspList.AgeGender.Gender = CType(lstitem.CaspList.AgeGender.Gender - CASPartResource.GenderFlags.Female, CASPartResource.GenderFlags)

            End If
            lstitem.IsModified = True
        Next

    End Sub

    Private Sub ckbMale__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbMale.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbMale.Checked = True Then

                lstitem.CaspList.AgeGender.Gender = CType(lstitem.CaspList.AgeGender.Gender + CASPartResource.GenderFlags.Male, CASPartResource.GenderFlags)
            ElseIf ckbMale.Checked = False Then
                lstitem.CaspList.AgeGender.Gender = CType(lstitem.CaspList.AgeGender.Gender - CASPartResource.GenderFlags.Male, CASPartResource.GenderFlags)

            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbBaby__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbBaby.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbBaby.Checked = True Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age + CASPartResource.AgeFlags.Baby, CASPartResource.AgeFlags)

            ElseIf ckbBaby.Checked = False Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age - CASPartResource.AgeFlags.Baby, CASPartResource.AgeFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbToddler__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbToddler.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbToddler.Checked = True Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age + CASPartResource.AgeFlags.Toddler, CASPartResource.AgeFlags)

            ElseIf ckbToddler.Checked = False Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age - CASPartResource.AgeFlags.Toddler, CASPartResource.AgeFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbChild__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbChild.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbChild.Checked = True Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age + CASPartResource.AgeFlags.Child, CASPartResource.AgeFlags)

            ElseIf ckbChild.Checked = False Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age - CASPartResource.AgeFlags.Child, CASPartResource.AgeFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbTeen__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbTeen.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbTeen.Checked = True Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age + CASPartResource.AgeFlags.Teen, CASPartResource.AgeFlags)

            ElseIf ckbTeen.Checked = False Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age - CASPartResource.AgeFlags.Teen, CASPartResource.AgeFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbYoungAdult__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbYoungAdult.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbYoungAdult.Checked = True Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age + CASPartResource.AgeFlags.YoungAdult, CASPartResource.AgeFlags)

            ElseIf ckbYoungAdult.Checked = False Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age - CASPartResource.AgeFlags.YoungAdult, CASPartResource.AgeFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbAdult__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbAdult.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbAdult.Checked = True Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age + CASPartResource.AgeFlags.Adult, CASPartResource.AgeFlags)

            ElseIf ckbAdult.Checked = False Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age - CASPartResource.AgeFlags.Adult, CASPartResource.AgeFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbElder_MouseClick(sender As Object, e As MouseEventArgs) Handles ckbElder.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbElder.Checked = True Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age + CASPartResource.AgeFlags.Elder, CASPartResource.AgeFlags)

            ElseIf ckbElder.Checked = False Then
                lstitem.CaspList.AgeGender.Age = CType(lstitem.CaspList.AgeGender.Age - CASPartResource.AgeFlags.Elder, CASPartResource.AgeFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbTop__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbTop.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbTop.Checked = True Then

                lstitem.CaspList.Clothing = CASPartResource.ClothingType.Top

            ElseIf ckbTop.Checked = False Then
                lstitem.CaspList.Clothing = CType(lstitem.CaspList.Clothing - CASPartResource.ClothingType.Top, CASPartResource.ClothingType)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbBottom__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbBottom.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbBottom.Checked = True Then
                lstitem.CaspList.Clothing = CASPartResource.ClothingType.Bottom

            ElseIf ckbBottom.Checked = False Then
                lstitem.CaspList.Clothing = CType(lstitem.CaspList.Clothing - CASPartResource.ClothingType.Bottom, CASPartResource.ClothingType)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbBody__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbBody.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbBody.Checked = True Then
                lstitem.CaspList.Clothing = CASPartResource.ClothingType.Body

            ElseIf ckbBody.Checked = False Then
                lstitem.CaspList.Clothing = CType(lstitem.CaspList.Clothing - CASPartResource.ClothingType.Body, CASPartResource.ClothingType)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbShoes__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbShoes.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbShoes.Checked = True Then
                lstitem.CaspList.Clothing = CASPartResource.ClothingType.Shoes

            ElseIf ckbShoes.Checked = False Then
                lstitem.CaspList.Clothing = CType(lstitem.CaspList.Clothing - CASPartResource.ClothingType.Shoes, CASPartResource.ClothingType)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbHair__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbHair.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbHair.Checked = True Then
                lstitem.CaspList.Clothing = CASPartResource.ClothingType.Hair
                'lstitem.CaspList.Clothing = CType(lstitem.CaspList.Clothing Or CASPartResource.ClothingType.Hair, CASPartResource.ClothingType)

            ElseIf ckbHair.Checked = False Then
                lstitem.CaspList.Clothing = CType(lstitem.CaspList.Clothing And Not CASPartResource.ClothingType.Hair, CASPartResource.ClothingType)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbEveryday_MouseClick(sender As Object, e As MouseEventArgs) Handles ckbEveryday.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbEveryday.Checked = True Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory Or CASPartResource.ClothingCategoryFlags.Everyday, CASPartResource.ClothingCategoryFlags)

            ElseIf ckbEveryday.Checked = False Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory And Not CASPartResource.ClothingCategoryFlags.Everyday, CASPartResource.ClothingCategoryFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbFormalwear_MouseClick(sender As Object, e As MouseEventArgs) Handles ckbFormalwear.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbFormalwear.Checked = True Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory Or CASPartResource.ClothingCategoryFlags.Formalwear, CASPartResource.ClothingCategoryFlags)

            ElseIf ckbFormalwear.Checked = False Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory And Not CASPartResource.ClothingCategoryFlags.Formalwear, CASPartResource.ClothingCategoryFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbSleepwear_MouseClick(sender As Object, e As MouseEventArgs) Handles ckbSleepwear.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbSleepwear.Checked = True Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory Or CASPartResource.ClothingCategoryFlags.Sleepwear, CASPartResource.ClothingCategoryFlags)

            ElseIf ckbSleepwear.Checked = False Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory And Not CASPartResource.ClothingCategoryFlags.Sleepwear, CASPartResource.ClothingCategoryFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbSwimwear__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbSwimwear.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbSwimwear.Checked = True Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory Or CASPartResource.ClothingCategoryFlags.Swimwear, CASPartResource.ClothingCategoryFlags)

            ElseIf ckbSwimwear.Checked = False Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory And Not CASPartResource.ClothingCategoryFlags.Swimwear, CASPartResource.ClothingCategoryFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbAthletic__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbAthletic.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbAthletic.Checked = True Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory Or CASPartResource.ClothingCategoryFlags.Athletic, CASPartResource.ClothingCategoryFlags)

            ElseIf ckbAthletic.Checked = False Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory And Not CASPartResource.ClothingCategoryFlags.Athletic, CASPartResource.ClothingCategoryFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbOuterwear__MouseClick(sender As Object, e As MouseEventArgs) Handles ckbOuterwear.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbOuterwear.Checked = True Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory Or CASPartResource.ClothingCategoryFlags.Outerwear, CASPartResource.ClothingCategoryFlags)

            ElseIf ckbOuterwear.Checked = False Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory And Not CASPartResource.ClothingCategoryFlags.Outerwear, CASPartResource.ClothingCategoryFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbValidForRandom_MouseClick(sender As Object, e As MouseEventArgs) Handles ckbValidForRandom.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbValidForRandom.Checked = True Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory Or CASPartResource.ClothingCategoryFlags.ValidForRandom, CASPartResource.ClothingCategoryFlags)

            ElseIf ckbValidForRandom.Checked = False Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory And Not CASPartResource.ClothingCategoryFlags.ValidForRandom, CASPartResource.ClothingCategoryFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbIsHiddenInCAS_MouseClick(sender As Object, e As MouseEventArgs) Handles ckbIsHiddenInCAS.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbIsHiddenInCAS.Checked = True Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory Or CASPartResource.ClothingCategoryFlags.IsHiddenInCAS, CASPartResource.ClothingCategoryFlags)

            ElseIf ckbIsHiddenInCAS.Checked = False Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory And Not CASPartResource.ClothingCategoryFlags.IsHiddenInCAS, CASPartResource.ClothingCategoryFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbValidForMaternity_MouseClick(sender As Object, e As MouseEventArgs) Handles ckbValidForMaternity.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems
            If ckbValidForMaternity.Checked = True Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory Or CASPartResource.ClothingCategoryFlags.ValidForMaternity, CASPartResource.ClothingCategoryFlags)

            ElseIf ckbValidForMaternity.Checked = False Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory And Not CASPartResource.ClothingCategoryFlags.ValidForMaternity, CASPartResource.ClothingCategoryFlags)
            End If
            lstitem.IsModified = True
        Next
    End Sub

    Private Sub ckbNaked_MouseClick(sender As Object, e As MouseEventArgs) Handles ckbNaked.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems

            If ckbNaked.Checked = True Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory Or CASPartResource.ClothingCategoryFlags.Naked, CASPartResource.ClothingCategoryFlags)

            ElseIf ckbNaked.Checked = False Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory And Not CASPartResource.ClothingCategoryFlags.Naked, CASPartResource.ClothingCategoryFlags)
            End If
            lstitem.IsModified = True

        Next

    End Sub

    Private Sub ckbCareer_MouseClick(sender As Object, e As MouseEventArgs) Handles ckbCareer.MouseClick
        For Each lstitem As LstItem In lstView.SelectedItems

            If ckbCareer.Checked = True Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory Or CASPartResource.ClothingCategoryFlags.Career, CASPartResource.ClothingCategoryFlags)

            ElseIf ckbCareer.Checked = False Then
                lstitem.CaspList.ClothingCategory = CType(lstitem.CaspList.ClothingCategory And Not CASPartResource.ClothingCategoryFlags.Career, CASPartResource.ClothingCategoryFlags)
            End If
            lstitem.IsModified = True

        Next
    End Sub


#End Region




    Private Sub coboxPack_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles coboxPack.SelectionChangeCommitted

        Save()
        PackName = coboxPack.Text
        Reset()
        list.Clear()
        btnLeft.Enabled = False
        btnRight.Enabled = False
        RightCount = 0
        LeftCount = 0
        imgList.Images.Clear()
        lstView.Items.Clear()
        PicBox.Image = Nothing

        'This is to clear the UI
        Wait.Show()
        Wait.Focus()
        Me.Enabled = False



        'bgwLoad_DoWork(newPacktoRead, Nothing)
        bgwLoad.RunWorkerAsync()

    End Sub



    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub




    Private Sub SetCreatorsNameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SetCreatorsNameToolStripMenuItem.Click
        If SetUserName.ShowDialog = Windows.Forms.DialogResult.OK Then
            If My.Computer.FileSystem.FileExists(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package")) = True Then
                Rename(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"), Path.Combine(overridePath, SetUserName.TextBox1.Text & "_CASP.package"))
            End If

            My.Settings.UserName = SetUserName.TextBox1.Text
        End If
    End Sub




    ''' <summary>
    ''' This is to delete the info of selected item
    ''' </summary>
    Private Sub ResetSelectedItemToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ResetSelectedItemToolStripMenuItem.Click
        If lstView.SelectedItems.Count > 0 Then
            Dim lstitem As LstItem = CType(lstView.SelectedItems(0), CASP_Editor.LstItem)
            ResetSelectedItem(lstitem)
        End If
    End Sub

    Public Sub ResetSelectedItem(ByVal item As LstItem)
        If My.Computer.FileSystem.FileExists(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package")) = True Then
            UserSet = Package.OpenPackage(0, Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"), True)
            For Each entry As IResourceIndexEntry In UserSet.GetResourceList
                If entry.Instance = item.Instance Then
                    UserSet.DeleteResource(entry)
                End If
            Next
            UserSet.SavePackage()
            item.CaspList = New CASPartResource.CASPartResource(0, WrapperDealer.GetResource(0, newPacktoRead, newPacktoRead.Find(Function(Entry As IResourceIndexEntry) Entry.Instance.Equals(item.Instance)), True).Stream)
            item.IsModified = False
        End If

    End Sub

    Private Sub ResetAllChangesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ResetAllChangesToolStripMenuItem.Click
        'This is to close the userset
        'Package.ClosePackage(1, UserSet)
        If My.Computer.FileSystem.FileExists(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package")) = True AndAlso _
            MessageBox.Show("Are you sure to reset all the changes?", "Attention", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            For Each entry As IResourceIndexEntry In UserSet.GetResourceList
                UserSet.DeleteResource(entry)

            Next
            UserSet.SavePackage()
            MessageBox.Show("Reset completed")
        End If

    End Sub




    Private Sub ShowUserPackageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ShowUserPackageToolStripMenuItem.Click
        If My.Computer.FileSystem.FileExists(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package")) = True Then
            Process.Start("explorer.exe", "/select," & Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"))
        End If
    End Sub

    Private Sub SetSort()
        For Each ckb As CheckBox In Me.GroupBox2.Controls
            combMain.Items.Add(ckb.Text)
        Next

        For Each ckb As CheckBox In Me.GroupBox3.Controls
            combMain.Items.Add(ckb.Text)
        Next

        For Each ckb As CheckBox In Me.GroupBox4.Controls
            comboSort.Items.Add(ckb.Text)
        Next
    End Sub

    Private Sub comboSort_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles comboSort.SelectionChangeCommitted
        Save()
        PackName = coboxPack.Text
        Type = comboSort.Text

        Reset()
        list.Clear()
        btnLeft.Enabled = False
        btnRight.Enabled = False
        RightCount = 0
        LeftCount = 0
        imgList.Images.Clear()
        lstView.Items.Clear()
        PicBox.Image = Nothing

        Wait.Show()
        Wait.Focus()
        Me.Enabled = False

        bgwLoad.RunWorkerAsync()
    End Sub

    Private Sub combMain_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles combMain.SelectionChangeCommitted
        Save()
        PackName = coboxPack.Text
        Main = combMain.Text

        Reset()
        list.Clear()
        btnLeft.Enabled = False
        btnRight.Enabled = False
        RightCount = 0
        LeftCount = 0
        imgList.Images.Clear()
        lstView.Items.Clear()
        PicBox.Image = Nothing

        Wait.Show()
        Wait.Focus()
        Me.Enabled = False

        bgwLoad.RunWorkerAsync()
    End Sub



    Private Sub ShowHelpToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ShowHelpToolStripMenuItem.Click
        Help.Show()
    End Sub

    Private Sub GetSupportToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GetSupportToolStripMenuItem.Click
        Dim theUsecCulture = System.Globalization.CultureInfo.CurrentCulture
        If theUsecCulture.ToString.Contains("zh") = False Then
            Process.Start("http://www.den.simlogical.com/denforum/index.php?topic=1589.0")
        Else
            Process.Start("http://bbs.3dmgame.com/thread-3620658-1-1.html")
        End If



    End Sub

    Private Sub bgwLoad_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwLoad.RunWorkerCompleted
        For Each Item As LstItem In ItemList
            lstView.Items.Add(Item)
        Next

        lblCount.Text = CStr(ItemList.Count)
        Wait.Dispose()


        Me.Enabled = True
        Me.Show()

    End Sub



    Private Sub Output_Click(sender As Object, e As EventArgs) Handles Output.Click
        Dim save As New SaveFileDialog
        With save
            .Filter = "Package|*.package"
            .OverwritePrompt = False
        End With
        For Each LstItem As LstItem In lstView.SelectedItems
            If save.ShowDialog = Windows.Forms.DialogResult.OK Then
                If My.Computer.FileSystem.FileExists(save.FileName) = True And Path.GetExtension(save.FileName) = ".package" Then
                    Dim newPack As IPackage = Package.OpenPackage(1, save.FileName, True)
                    For Each entry As IResourceIndexEntry In newPack.GetResourceList
                        If entry.Instance = LstItem.Instance Then
                            If MessageBox.Show(Localize.Localize("I detect that you have made same changes to the same thing. Click yes to override the original one"), Localize.Localize("Attention"), MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                                newPack.DeleteResource(entry)
                            End If
                        End If
                    Next
                    newPack.AddResource(LstItem.entryKey, LstItem.CaspList.Stream, True)
                    newPack.SavePackage()
                    Package.ClosePackage(1, newPack)
                    MessageBox.Show(Localize.Localize("Save successfully!"))
                ElseIf My.Computer.FileSystem.FileExists(save.FileName) = False Then
                    Dim newPack As IPackage = Package.NewPackage(1)
                    newPack.AddResource(LstItem.entryKey, LstItem.CaspList.Stream, True)
                    newPack.SaveAs(save.FileName)
                    Package.ClosePackage(1, newPack)
                    MessageBox.Show(Localize.Localize("Save successfully!"))
                End If
            End If
        Next
    End Sub

    Private Sub ViewUserPackageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewUserPackageToolStripMenuItem.Click
        ShowUserPackageToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub Support_Click(sender As Object, e As EventArgs) Handles Support.Click
        GetSupportToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub ClearMissingCASPToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ClearMissingCASPToolStripMenuItem.Click
        If MessageBox.Show(Localize.Localize("Clearing missing CASP needs some time, are you sure to continue?"), Localize.Localize("Attention"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Wait.Show()
            Wait.Focus()
            Me.Enabled = False
            bgwClear.RunWorkerAsync()
        End If

    End Sub

    Private Sub bgwClear_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgwClear.DoWork
        If My.Computer.FileSystem.FileExists(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package")) = True Then
            UserSet = Package.OpenPackage(1, Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"), True)
        Else
            UserSet = Package.NewPackage(1)
            UserSet.SaveAs(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"))
            UserSet = Package.OpenPackage(1, Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"), True)
        End If


        For Each entry As IResourceIndexEntry In UserSet.GetResourceList
            Dim isMissing As Integer = 0
            For Each strPack As String In coboxPack.Items
                Dim pack As IPackage
                If InStr(strPack, "Content") = 0 Then
                    pack = GetEAstuff(strPack)
                Else
                    pack = packInteraction.GetDBCpackage
                End If

                Dim casp As ULong() = {entry.Instance}
                Dim entries As IList(Of IResourceIndexEntry) = pack.FindAll(Function(entrytocompare As IResourceIndexEntry) casp.Contains(entrytocompare.Instance) And entrytocompare.ResourceType = &H34AEECB)
                If entries.Count = 0 Then
                    isMissing += 1
                End If
            Next

            If isMissing >= coboxPack.Items.Count Then
                UserSet.DeleteResource(entry)
            End If
        Next
        UserSet.SavePackage()


    End Sub

    Private Sub bgwClear_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwClear.RunWorkerCompleted
        MessageBox.Show("Missing CASP has been cleared")
        Wait.Dispose()
        Me.Enabled = True
        Me.Show()
    End Sub

    Private Sub bgwSave_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgwSave.DoWork
        Dim listSave As List(Of LstItem) = DirectCast(e.Argument, List(Of LstItem))
        If DirectCast(listSave, ICollection).Count > 0 Then
            If My.Computer.FileSystem.FileExists(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package")) = True Then
                UserSet = Package.OpenPackage(1, Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"), True)
            Else
                UserSet = Package.NewPackage(1)
                UserSet.SaveAs(Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"))
                UserSet = Package.OpenPackage(1, Path.Combine(overridePath, My.Settings.UserName & "_CASP.package"), True)
            End If




            For Each LstItem As LstItem In listSave
                For Each entry As IResourceIndexEntry In UserSet.GetResourceList
                    If entry.Instance = LstItem.Instance Then
                        If MessageBox.Show(Localize.Localize("I detect that you have made same changes to the same thing. Click yes to override the original one"), Localize.Localize("Attention"), MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                            UserSet.DeleteResource(entry)
                        End If
                    End If
                Next
                UserSet.AddResource(LstItem.entryKey, LstItem.CaspList.Stream, True)

                'set the hash code to default
                LstItem.OriginalHash = LstItem.CaspList.ToString.GetHashCode

            Next
        End If

        For Each entry As IResourceIndexEntry In UserSet.GetResourceList
            If entry.Compressed = 0 Then
                entry.Compressed = &HFFFF
            End If
        Next

        If UserSet.GetResourceList.Count > 0 Then
            UserSet.SavePackage()

        End If
    End Sub

    Private Sub bgwSave_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwSave.RunWorkerCompleted
        Wait.Dispose()
        Me.Enabled = True
        Me.Show()
        Me.Focus()
    End Sub



    Private Sub HideHiddenInCASItemToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HideHiddenInCASItemToolStripMenuItem.Click
        My.Settings.Hidden = HideHiddenInCASItemToolStripMenuItem.Checked
        My.Settings.Save()
        If My.Settings.Hidden = True Then
            lstView.Items.Clear()
            For Each Item As LstItem In ItemList
                If Item.CaspList.ClothingCategory.ToString().Contains("Hidden") = False Then
                    lstView.Items.Add(Item)
                End If
            Next
        Else
            lstView.Items.Clear()
            For Each Item As LstItem In ItemList

                lstView.Items.Add(Item)

            Next
        End If
        lblCount.Text = lstView.Items.Count.ToString()
    End Sub

    Private Sub ckbIsModified_CheckedChanged(sender As Object, e As EventArgs) Handles ckbIsModified.CheckedChanged
        If lstView.Items.Count > 0 Then
            Dim item As LstItem = DirectCast(lstView.SelectedItems(0), LstItem)
            If item.IsModified = True AndAlso ckbIsModified.Checked = False Then
                ResetSelectedItem(item)
            End If
        End If
    End Sub
End Class
